import { login, quest } from "../../utils/http";

// pages/me/me.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: [],
    collages: [],
    index: 0,
    myCollage: "",
    showModal: false,
    isAdmin: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let collage = await quest({
      url: "university/list",
    });
    if (collage.code != 200) {
      return;
    }
    let userInfo = wx.getStorageSync("userInfo");
    this.setData({
      userInfo,
      collages: collage.data.items,
    });
    this.getMyCollage();
  },
  async getMyCollage() {
    let data = await this.getClaim();
    this.data.collages.forEach((i) => {
      if (i.id == data.universityId) {
        this.setData({
          myCollage: i.university,
        });
      }
    });
  },
  // 选择器校区
  bindPickerChange: function (e) {
    let token = wx.getStorageSync("token");
    console.log(token);
    if (token == "") {
      wx.showModal({
        cancelText: "取消",
        confirmText: "确定",
        content: "请先登录",
        editable: false,
        showCancel: false,
        title: "提示",
        success: (result) => {},
        fail: (res) => {},
        complete: (res) => {},
      });
      return;
    }
    // console.log(this.data.collages[e.detail.value]);
    this.selectCollage(e.detail.value);
  },
  async selectCollage(index) {
    let claim = await this.getClaim();
    let collage = this.data.collages[index];
    let data = await quest({
      url: "user/update",
      method: "post",
      data: {
        id: claim.uid,
        universityId: collage.id,
      },
    });
    this.data.collages.forEach((i) => {
      if (i.id == data.data.universityId) {
        this.setData({
          myCollage: i.university,
        });
      }
    });
    let code = await login();
    let token = await quest({
      url: "token",
      method: "POST",
      data: {
        openId: code,
      },
    });
    console.log(token);
    wx.setStorageSync("token", "Bearer " + token.token);
  },
  addOrder() {
    let token = wx.getStorageSync("token");

    console.log(token);
    if (token == "") {
      wx.showModal({
        cancelText: "取消",
        confirmText: "确定",
        content: "请先登录",
        editable: false,
        showCancel: false,
        title: "提示",
        success: (result) => {},
        fail: (res) => {},
        complete: (res) => {},
      });
      return;
    }
    wx.navigateTo({
      url: "/pages/addOrder/addOrder",
    });
  },
  myOrder() {
    let token = wx.getStorageSync("token");
    if (token == "") {
      wx.showModal({
        cancelText: "取消",
        confirmText: "确定",
        content: "请先登录",
        editable: false,
        showCancel: false,
        title: "提示",
        success: (result) => {},
        fail: (res) => {},
        complete: (res) => {},
      });
      return;
    }
    wx.navigateTo({
      url: "/pages/myOrder/myOrder",
    });
  },
  async login(e) {
    let user = await wx.getUserProfile({
      desc: "登录",
    });
    let code = await login();
    // console.log(user);
    // 获取token
    let token = await quest({
      url: "token",
      method: "POST",
      data: {
        openId: code,
        avatar: user.userInfo.avatarUrl,
        nickName: user.userInfo.nickName,
        gender: user.userInfo.gender,
      },
    });

    console.log(token);
    wx.setStorageSync("token", "Bearer " + token.token);
    wx.setStorageSync("userInfo", user.userInfo);
    let data = await this.getClaim();
    let isAdmin = data.scope >= 5;

    this.setData({
      userInfo: user.userInfo,
      isAdmin,
    });
    this.getMyCollage();
  },
  logout() {
    wx.setStorageSync("token", "");
    this.setData({
      userInfo: "",
      myCollage: "",
      isAdmin: false,
    });
  },
  selectCollege() {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    let data = await this.getClaim();
    let isAdmin = data.scope >= 5;
    this.setData({
      isAdmin,
    });
    // 有时候load加载慢为空
    // console.log(this.data.collages);
    this.data.collages.forEach((i) => {
      if (i.id == data.universityId) {
        this.setData({
          myCollage: i.university,
        });
      }
    });
  },
  async getClaim() {
    return await quest({
      url: "token/getClaim",
      method: "post",
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  eject: function () {
    let token = wx.getStorageSync("token");
    console.log(token);
    if (token == "") {
      wx.showModal({
        cancelText: "取消",
        confirmText: "确定",
        content: "请先登录",
        editable: false,
        showCancel: false,
        title: "提示",
        success: (result) => {},
        fail: (res) => {},
        complete: (res) => {},
      });
      return;
    }
    this.setData({
      showModal: true,
    });
  },
  /**
   * 点击返回按钮隐藏
   */
  back: function () {
    this.setData({
      showModal: false,
    });
  },
  /**
   * 获取input输入值
   */
  wish_put: function (e) {
    this.setData({
      textV: e.detail.value,
    });
  },
  /**
   * 点击确定按钮获取input值并且关闭弹窗
   */
  async name() {
    // console.log(this.data.textV);
    if (this.data.textV.length !== 11) {
      return;
    }
    let claim = await this.getClaim();
    let data = await quest({
      url: "user/update",
      method: "post",
      data: {
        id: claim.uid,
        phone: this.data.textV,
      },
    });
    this.setData({
      showModal: false,
      name: this.data.textV,
    });
  },
  goAdmin() {
    wx.navigateTo({
      url: "/pages/admin/admin",
    });
  },
  contact() {
    wx.showModal({
      confirmText: "确定",
      content: `QQ:1067559930
      VX:mxh56920`,
      editable: false,
      showCancel: false,
      title: "联系我们",
      success: (result) => {},
      fail: (res) => {},
      complete: (res) => {},
    });
  },
});
